<template>
  <el-card class="box-card">
    <div style="margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between">
      <div>
        <el-row>
          <el-col :span="22">
            <el-input v-model="urlParams.search" placeholder="请输入关键字" @keyup.enter="onSearch" clearable @clear="onSearch" class="search" />
          </el-col>
          <el-col :span="1" style="margin-left: 5px">
            <el-button type="primary" @click="onSearch">
              <el-icon><search /></el-icon>
              &nbsp;搜索
            </el-button>
          </el-col>
        </el-row>
      </div>
    </div>
    <div>
      <!--数据表格-->
      <el-table :data="RolesData" border style="width: 100%" :header-cell-style="{ backgroundColor: '#409EFF', color: '#fff', fontsize: '14px' }">
        <el-table-column prop="role" label="角色名称" />
        <el-table-column prop="name" label="角色描述" />
        <el-table-column prop="create_time" label="创建时间" :formatter="dateFormat"/>
        <el-table-column prop="update_time" label="修改时间" :formatter="dateFormat"/>
        <!--操作栏-->
        <el-table-column label="操作栏" fixed="right" width="180">
          <!--定义获取行内数据参数-->
          <template #default="scope">
            <!--通过回调函数获取行内数据-->
            <el-button type="primary" size="small" round @click="handelRolesEdit(scope.$index, scope.row)">权限绑定</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <!--分页-->
      <div style="margin-top: 20px">
        <el-pagination v-model:currentPage="currentPage" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total" background @size-change="handleSizeChange" @current-change="handleCurrentChange"></el-pagination>
      </div>
    </div>
  </el-card>
  <!--使用子组件-->
  <RolesEdit v-model:visible="dialogRolesEdit" :row="row"></RolesEdit>
</template>

<script>
import RolesEdit from './RolesEdit.vue'

export default {
  name: 'Roles',
   
  // 引用子组件
  components: {
    RolesEdit
  },
  data() {
    return {
      RolesData: [], // 存放数据表格列表
      
      // ================================ 分页配置 ============
      currentPage: 1, // 默认开始页面
      pageSize: 10, // 默认每页的数据条数
      total: 0, // 总数据条数
      urlParams: {
        // URL查询参数，传递服务端，放这里方便修改值
        page_num: 1,
        page_size: 10,
        search: ''
      },
      // =============================== 通用获取行内数据配置 ===============
      row: '',

      // =============================== 编辑配置 ===============
      dialogRolesEdit: false,
    }
  },
  // 页面渲染完后挂载
  mounted() {
    this.GetAllRoles()
  },
  // 请求方法
  methods: {
    GetAllRoles() {
      this.$http.get('api/roles/', { params: this.urlParams }).then(res => {
        if (res.data.code == 200) {
          // 把获取数据重新赋值到表格
          this.RolesData = res.data.data

          // 把获取的数据总量重新赋值给total
          this.total = res.data.count
          this.$message.success('获取数据成功')
        } else {
          this.$message.info('请求数据接口失败')
        }
      })
    },

    // 搜索查询
    onSearch() {
      // 获取搜索后的数据
      this.GetAllRoles()
    },

    // 编辑进行数据重新赋值
    handelRolesEdit(index, row) {
      // 重新赋值允许打开编辑弹出框
      this.dialogRolesEdit = true
      this.row = row
    },
    // 分页：监听【选择每页数量】的事件
    handleSizeChange(pageSize) {
      // console.log(pageSize)
      this.pageSize = pageSize // 重新设置分页显示
      this.urlParams.page_size = pageSize // 将最新值设置到数据里，用于下面用新值重新获取数据
      this.GetAllRoles()
    },

    // 分页：监听【点击页码】的事件
    handleCurrentChange(currentPage) {
      // console.log(currentPage)
      this.currentPage = currentPage // 重新设置分页显示
      this.urlParams.page_num = currentPage
      this.GetAllRoles()
    },
    
    // drf返回时间戳进行转换为标准显示格式
    dateFormat(row, col) {
      let data = row[col.property]
      if (data == null) {
        return ''
      }

      let currDate = new Date(data)
      // 例如 2022-8-15 10:21:30
      let getMonth = currDate.getMonth() + 1
      let getDay = currDate.getDate()
      let getHours = currDate.getHours()
      let getMinutes = currDate.getMinutes()
      let getSeconds = currDate.getSeconds()
      if (getMonth < 10) {
        getMonth = '0' + getMonth
      }
      if (getDay < 10) {
        getDay = '0' + getDay
      }
      if (getHours < 10) {
        getHours = '0' + getHours
      }
      if (getMinutes < 10) {
        getMinutes = '0' + getMinutes
      }
      if (getSeconds < 10) {
        getSeconds = '0' + getSeconds
      }
      // 组合进行返回时间操作
      return currDate.getFullYear() + '-' + getMonth + '-' + getDay + ' ' + getHours + ':' + getMinutes + ':' + getSeconds
    }
  }
}
</script>

<style scoped></style>
